/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFrameRegistrarProvedor.java
 *
 * Created on 24/03/2011, 08:17:21 PM
 */

package Vista;

import Controlador.ControlProveedor;
import Modelo.Proveedor;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author SAW
 */
public class ProveedorRegistrar extends javax.swing.JFrame {
    ControlProveedor Proveedor=new ControlProveedor();

    /** Creates new form JFrameRegistrarProvedor */
    public ProveedorRegistrar() throws SQLException {
        initComponents();        
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jTextCedulaNit = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextRazonSocial = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextEstablecimiento = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextTelefono = new javax.swing.JTextField();
        jButtonRegistrar = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextRepresentanteLegal = new javax.swing.JTextField();
        jButtonSalir = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.setName("jPanel2"); // NOI18N

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(0, 51, 0), new java.awt.Color(0, 51, 0), null, null));
        jPanel4.setName("jPanel4"); // NOI18N

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(0, 51, 0)));
        jPanel3.setName("jPanel3"); // NOI18N

        jLabel3.setBackground(new java.awt.Color(255, 255, 255));
        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setForeground(new java.awt.Color(51, 0, 0));
        jLabel3.setText("Cedula/Nit:");
        jLabel3.setName("jLabel3"); // NOI18N

        jTextCedulaNit.setName("jTextCedulaNit"); // NOI18N
        jTextCedulaNit.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextCedulaNitKeyPressed(evt);
            }
        });

        jLabel4.setBackground(new java.awt.Color(255, 255, 255));
        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setForeground(new java.awt.Color(51, 0, 0));
        jLabel4.setText("Razon Social:");
        jLabel4.setName("jLabel4"); // NOI18N

        jTextRazonSocial.setEnabled(false);
        jTextRazonSocial.setName("jTextRazonSocial"); // NOI18N
        jTextRazonSocial.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextRazonSocialKeyPressed(evt);
            }
        });

        jLabel5.setBackground(new java.awt.Color(255, 255, 255));
        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setForeground(new java.awt.Color(51, 0, 0));
        jLabel5.setText("Establecimiento:");
        jLabel5.setName("jLabel5"); // NOI18N

        jTextEstablecimiento.setEnabled(false);
        jTextEstablecimiento.setName("jTextEstablecimiento"); // NOI18N
        jTextEstablecimiento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextEstablecimientoKeyPressed(evt);
            }
        });

        jLabel7.setBackground(new java.awt.Color(255, 255, 255));
        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel7.setForeground(new java.awt.Color(51, 0, 0));
        jLabel7.setText("Telefono:");
        jLabel7.setName("jLabel7"); // NOI18N

        jTextTelefono.setEnabled(false);
        jTextTelefono.setName("jTextTelefono"); // NOI18N
        jTextTelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextTelefonoKeyPressed(evt);
            }
        });

        jButtonRegistrar.setFont(new java.awt.Font("Tahoma", 0, 14));
        jButtonRegistrar.setForeground(new java.awt.Color(51, 0, 0));
        jButtonRegistrar.setText("Registrar");
        jButtonRegistrar.setEnabled(false);
        jButtonRegistrar.setName("jButtonRegistrar"); // NOI18N
        jButtonRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegistrarActionPerformed(evt);
            }
        });
        jButtonRegistrar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButtonRegistrarKeyPressed(evt);
            }
        });

        jButtonCancelar.setFont(new java.awt.Font("Tahoma", 0, 14));
        jButtonCancelar.setForeground(new java.awt.Color(51, 0, 0));
        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.setName("jButtonCancelar"); // NOI18N
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });
        jButtonCancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButtonCancelarKeyPressed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Representante Legal:");
        jLabel2.setName("jLabel2"); // NOI18N

        jTextRepresentanteLegal.setEnabled(false);
        jTextRepresentanteLegal.setName("jTextRepresentanteLegal"); // NOI18N
        jTextRepresentanteLegal.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextRepresentanteLegalKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel7))
                .addGap(15, 15, 15)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextRazonSocial, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
                    .addComponent(jTextRepresentanteLegal, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
                    .addComponent(jTextEstablecimiento, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
                    .addComponent(jTextCedulaNit, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jTextTelefono, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonRegistrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCancelar)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextCedulaNit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextRazonSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextRepresentanteLegal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextEstablecimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonCancelar)
                        .addComponent(jButtonRegistrar))
                    .addComponent(jLabel7)
                    .addComponent(jTextTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(12, Short.MAX_VALUE)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jButtonSalir.setForeground(new java.awt.Color(51, 0, 0));
        jButtonSalir.setText("Finalizar");
        jButtonSalir.setName("jButtonSalir"); // NOI18N
        jButtonSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalirActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/rutascoronasa/resources/Imagenes/registro de proveedores.png"))); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonSalir, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextCedulaNitKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextCedulaNitKeyPressed
        if(evt.getKeyCode()==10)
            verificarProveedor();
}//GEN-LAST:event_jTextCedulaNitKeyPressed

    private void jTextRazonSocialKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextRazonSocialKeyPressed
        if (evt.getKeyCode()==10){
            if(jTextRazonSocial.getText().trim().equalsIgnoreCase(""))
                JOptionPane.showMessageDialog(null, "Este Campo Es Requerido");
            else {
                jTextRepresentanteLegal.setEnabled(true);
                jTextRepresentanteLegal.requestFocus();
            }
        }
}//GEN-LAST:event_jTextRazonSocialKeyPressed

    private void jTextEstablecimientoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextEstablecimientoKeyPressed
        if(evt.getKeyCode()==10){
            if(jTextEstablecimiento.getText().trim().equalsIgnoreCase("")){
                JOptionPane.showMessageDialog(null, "Este Campo Es Requerido");
            }else{
                jTextTelefono.setEnabled(true);
                jTextTelefono.requestFocus();
            }
        }
}//GEN-LAST:event_jTextEstablecimientoKeyPressed

    private void jTextTelefonoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextTelefonoKeyPressed
        if(evt.getKeyCode()==10){
            jButtonRegistrar.setEnabled(true);
            jButtonRegistrar.requestFocus();
        }
    }//GEN-LAST:event_jTextTelefonoKeyPressed

    private void jButtonRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegistrarActionPerformed
        verificarProveedor();
}//GEN-LAST:event_jButtonRegistrarActionPerformed

    private void jButtonRegistrarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButtonRegistrarKeyPressed
        if(evt.getKeyCode()==10)
            registrar();
}//GEN-LAST:event_jButtonRegistrarKeyPressed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        clean();//limpia todos los textos e inhabilita
}//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jButtonCancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButtonCancelarKeyPressed
        if(evt.getKeyCode()==10)
            clean();
}//GEN-LAST:event_jButtonCancelarKeyPressed

    private void jTextRepresentanteLegalKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextRepresentanteLegalKeyPressed
        if (evt.getKeyCode()==10){
            if(jTextRepresentanteLegal.getText().trim().equalsIgnoreCase(""))
                JOptionPane.showMessageDialog(null, "Este Campo Es Requerido");
            else {
                jTextEstablecimiento.setEnabled(true);
                jTextEstablecimiento.requestFocus();
            }
        }
}//GEN-LAST:event_jTextRepresentanteLegalKeyPressed

    private void jButtonSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalirActionPerformed
        this.setVisible(false);
}//GEN-LAST:event_jButtonSalirActionPerformed
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonRegistrar;
    private javax.swing.JButton jButtonSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTextField jTextCedulaNit;
    private javax.swing.JTextField jTextEstablecimiento;
    private javax.swing.JTextField jTextRazonSocial;
    private javax.swing.JTextField jTextRepresentanteLegal;
    private javax.swing.JTextField jTextTelefono;
    // End of variables declaration//GEN-END:variables


    private void clean(){//limpio todos los campos de la vista
        jTextCedulaNit.setEnabled(true);
        jTextCedulaNit.setText("");
        jTextRepresentanteLegal.setEnabled(false);
        jTextRepresentanteLegal.setText("");
        jTextEstablecimiento.setEnabled(false);
        jTextEstablecimiento.setText("");
        jTextRazonSocial.setEnabled(false);
        jTextRazonSocial.setText("");
        jTextTelefono.setEnabled(false);
        jTextTelefono.setText("");
        jButtonRegistrar.setEnabled(false);
        jTextCedulaNit.requestFocus();
    }

    private void registrar(){//utilisado para verificar campos vacios y el registro del provedor
           if(jTextCedulaNit.getText().trim().equalsIgnoreCase("") ||
                   jTextRepresentanteLegal.getText().trim().equalsIgnoreCase("")||
                   jTextEstablecimiento.getText().trim().equalsIgnoreCase("")||
                   jTextRazonSocial.getText().trim().equalsIgnoreCase("")){
                JOptionPane.showMessageDialog(null, "Uno De Los Campos Es Requerido");
            }else{
                Proveedor prov=new Proveedor();
                prov.setNit(jTextCedulaNit.getText());
                prov.setRazonSocial(jTextCedulaNit.getText());
                prov.setRepresentanteLegal(jTextRazonSocial.getText());
                prov.setDireccion(jTextRepresentanteLegal.getText());
                prov.setTelefono(jTextTelefono.getText());
                prov.setEstado(true);                

                try {
                    Proveedor.setNuevoProveedor(prov);
                    JOptionPane.showMessageDialog(null,"Operacion Exitosa...");
                } catch (SQLException ex) {
                    Logger.getLogger(ProveedorRegistrar.class.getName()).log(Level.SEVERE, null, ex);
                }

                clean();
            }
    }

    public void verificarProveedor(){
         if(jTextCedulaNit.getText().trim().equalsIgnoreCase(""))
                JOptionPane.showMessageDialog(null,"Este Campo Es Requerido");
            else{
                try {                    
                    if (Proveedor.isExist(jTextCedulaNit.getText())) {
                        JOptionPane.showMessageDialog(null, "Provedor Se Encuentra Registrado");
                    } else {
                        jTextCedulaNit.setEnabled(false);
                        jTextRazonSocial.setEnabled(true);
                        jTextRazonSocial.requestFocus();
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(ProveedorRegistrar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    }

}
